<!-- 管理员用户组新增和编辑页面 -->
<extend name="Public/base"/>
<block name="body">
    <div class="main-title">
        <h2>{$top_title}云码类型</h2>
    </div>
    <div class="tab-wrap with-padding">
        <form action="__SELF__" method="post" class="form-horizontal">
            <label class="item-label">编号</label>
            <div class="controls ">
                <input type="hidden" name="id" value="{$data['id']}" class="text input-large form-control" style="width: 400px" placeholder="无需填写" readonly="">
                <p class="lead">{$data['id']}</p>
            </div>
            <label class="item-label">标题 </label>
            <div class="controls ">
                <input type="text" name="title" class="text input-large form-control" style="width: 400px" value="{$data['title']}">
            </div>

            <label class="item-label">绑定产品 </label>
            <div class="controls ">
                <div id="version_select">
                </div>
                <a href="javascript:" data-role="add_version">新增产品</a>
                <if condition="$data['id']">
                    <input type="text" name="version_ids" id="version_id" value="{$data['version_ids']}" class="text input-large form-control" style="width: 400px;">
                    <else/>
                    <input type="hidden" name="version_ids" id="version_id" class="text input-large form-control" style="width: 400px;">
                </if>
            </div>
            <label class="item-label">云码长度 </label>
            <div class="controls ">
                <input type="text" name="length" class="text input-large form-control" style="width: 400px" value="{$data['length']|default=11}">
            </div>
            <label class="item-label">状态</label>
            <div class="controls ">
                <select name="status" id="status" class="form-control" style="width:auto;">
                    <option value="-1">删除</option>
                    <option value="0">禁用</option>
                    <option value="1">启用</option>
                    <option value="2">未审核</option>
                </select>
                <script>
                    $(function(){
                        var now_status="{$data['status']|default=1}";
                        $('#status').val(now_status);
                    })
                </script>
            </div>
            <br>
            <div class="form-item">
                <button class="btn submit-btn ajax-post" id="submit" type="submit" target-form="form-horizontal">确定</button>
                &nbsp;
                <button onclick="javascript:history.back(-1);return false;" class="btn btn-return">返回</button>
                &nbsp;
            </div>
        </form>
    </div>
</block>
<block name="script">
    <script type="text/javascript" charset="utf-8">
        $(function(){
            $('[data-role="add_version"]').click(function(){
                var html='<div class="one_select" style="margin-bottom: 10px">\
                        <select class="form-control" data-role="type_select" style="width: 150px;display: inline-block">\
                        <option value="0">请选择类型</option>\
                <option value="1">插件</option>\
                <option value="2">模块</option>\
                <option value="3">主题</option>\
                <option value="4">服务</option>\
                </select>\
                <select class="form-control category-select" data-role="category_select" style="width: 150px;display: inline-block">\
                        <option value="0">请选择分类</option>\
                </select>\
                <select class="form-control goods-select" data-role="goods_select" style="width: 150px;display: inline-block">\
                        <option value="0">请选择商品</option>\
                </select>\
                <select class="form-control version-select" data-role="version_select" style="width: 150px;display: inline-block">\
                        <option value="0">请选择版本</option>\
                </select>\
                <input type="hidden" class="old-version">\
                <a href="javascript:" data-role="del_version">取消绑定</a>\
                </div>'
                $('#version_select').append(html);
                bind_version_select();
            });
            highlight_subnav('{:U('Appstore/editCode ')}');
        })
        var bind_version_select=function(){
            $('[data-role="type_select"]').unbind('click');
            $('[data-role="category_select"]').unbind('click');
            $('[data-role="goods_select"]').unbind('click');
            $('[data-role="version_select"]').unbind('click');
            $('[data-role="del_version"]').unbind('click');

            $('[data-role="type_select"]').change(function(){
                var that=$(this);
                var type=that.val();
                that.parents('.one_select').find('.goods-select').html('<option value="0">请选择商品</option>');
                that.parents('.one_select').find('.version-select').html('<option value="0">请选择版本</option>');
                upVersionVal();
                if(type==0){
                    that.parents('.one_select').find('.category-select').html('<option value="0">请选择分类</option>');
                }else{
                    $.post(U('Admin/Appstore/getCategoryByType'),{type:type},function(msg){
                        if(msg.status){
                            that.parents('.one_select').find('.category-select').html(msg.html);
                        }
                    },'json');
                }
            });
            $('[data-role="category_select"]').change(function(){
                var that=$(this);
                var category=that.val();
                that.parents('.one_select').find('.version-select').html('<option value="0">请选择版本</option>');
                upVersionVal();
                if(category==0){
                    that.parents('.one_select').find('.goods-select').html('<option value="0">请选择商品</option>');
                }else{
                    $.post(U('Admin/Appstore/getGoodsByCategory'),{category:category},function(msg){
                        if(msg.status){
                            that.parents('.one_select').find('.goods-select').html(msg.html);
                        }
                    },'json');
                }
            });
            $('[data-role="goods_select"]').change(function(){
                var that=$(this);
                var goods=that.val();
                if(goods==0){
                    that.parents('.one_select').find('.version-select').html('<option value="0">请选择版本</option>');
                }else{
                    $.post(U('Admin/Appstore/getVersionByGoods'),{goods:goods},function(msg){
                        if(msg.status){
                            that.parents('.one_select').find('.version-select').html(msg.html);
                        }
                    },'json');
                }
                upVersionVal();
            });
            $('[data-role="version_select"]').change(function(){
                upVersionVal();
            });

            $('[data-role="del_version"]').click(function(){
                $(this).parent('.one_select').remove();
                upVersionVal();
            });
        }

        var upVersionVal=function () {
            var newArr = [];
            $('[data-role="version_select"]').each(function(){
                var value=$(this).val();
                if(value!=''&&value!=0){
                    newArr.push(value.toString());
                }
            });
            var tag_ids=newArr.join(',');
            $('#version_id').val(tag_ids);
            return true;
        }
    </script>
</block>
